//
//  PlistUtil.swift
//  SwiftStudyDemoAll
//
//  Created by BINGO on 2019/5/12.
//  Copyright © 2019 mrlong. All rights reserved.
//


import Foundation
class PlistUtil {
    func put(fileName:String,key:String,val:String) -> Bool {
        do{
            //获得存储路径
            let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
            //拼接名字
            let fileName = "\(path!)/\(fileName).plist"
            let dic:NSDictionary = [key:val]
            //写入
            return dic.write(toFile: fileName, atomically: true)
        } catch {
            return false
        }
    }
    func get(fileName:String,key:String) -> String {
        //获得数据存储路径
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
        let fileNameRel = "\(path!)/\(fileName).plist"
        //判断文件是否存在
        let fileManager = FileManager.init()
        let exist = fileManager.fileExists(atPath: fileNameRel)
        if !exist{
            //print("你查询的文件不存在")
            return ""
        }
        //读取plist：如果有数返回数据 否则返回默认 ""
        let dicRes = NSDictionary.init(contentsOfFile: fileNameRel)
        if dicRes != nil{
            let res = dicRes?[key]
            return res as? String ?? ""
        }else{
            return ""
        }
    }
}
